<?php defined('SYSPATH') OR die('No direct script access.');

class Controller_Placeholder extends Controller {

	public function action_index($size = '100x100', $bgcolor = 'ccc', $color = '555', $text = '', $fontsize = NULL)
	{

		$this->response->headers('Content-Type', 'image/png');
		// Dimensions
		list($width, $height) = explode('x', $size);
		empty($height) && $height = $width;
		$bgcolor    = hex2rgb($bgcolor);
		$color         = hex2rgb($color);
		empty($text) && $text = $width . ' x ' .$height;

		$hash_key = md5(serialize(compact('width','height','bgcolor','color','text')));

		// Create image
		$image = imagecreate($width, $height);

		// Colours
		$setbg = imagecolorallocate($image, $bgcolor['r'], $bgcolor['g'], $bgcolor['b']);
		$fontcolor = imagecolorallocate($image, $color['r'], $color['g'], $color['b']);

		// Text positioning
		empty($fontsize) && $fontsize = ($width > $height) ? ($height / 10) : ($width / 10) ;
		$font = MODPATH.'addons/fonts/msyh.ttf';
		//$fontbox = imagettfbbox($fontsize, 0, $font, $text);
		$fontbox = calculate_textbox($fontsize, 0, $font, $text);
		// Generate text
		function_exists('imageantialias') && imageantialias($image, true);
		imagettftext($image, $fontsize, 0, ceil(($width - $fontbox['width'] ) / 2 + $fontbox['left']), ceil(($height - $fontbox['height']  ) / 2 + $fontbox['top']), $fontcolor, $font, $text);

		// Render image
		imagepng($image);

		imagedestroy($image);


	}
}